package controller;

import model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.StudentService;

import java.util.List;

@Controller
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @RequestMapping("list.do")
    public String list(Model model){
        List<Student> students=studentService.findAll();
        model.addAttribute("students",students);
        return "listStudents";
    }
    @RequestMapping("delete.do")
    public String delete(Model model,int id){
        studentService.remove(id);
        return list(model);
    }
    @RequestMapping("toAdd.do")
    public String toAdd(){
        return "addStudent";
    }
    @RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model){
        Student student=studentService.findById(id);
        model.addAttribute("student",student);
        return "updateStudent";
    }
    @RequestMapping("add.do")
    public String add(Student student,Model model){
        studentService.save(student);
        return list(model);
    }
    @RequestMapping("update.do")
    public String update(Student student,Model model){
        studentService.update(student.getId(),student);
        return list(model);
    }
}
